class ObserverList {
  constructor() {
    this.observerList = [];
  }
  add(ob) {
    this.observerList.push(ob);
  }
  count() {
    return this.observerList.length;
  }
  del(ob) {
    const index = this.observerList.findIndex((f) => f === ob);
    return this.observerList.splice(index, 1);
  }
}

class Subject {
  constructor() {
    this.observer = new ObserverList();
  }
  register(ob) {
    this.observer.add(ob);
  }
  remove(ob) {
    this.observer.del(ob);
  }
  notify(params) {
    for (let index = 0; index < this.observer.count(); index++) {
      this.observer.observerList[index].update(params);
    }
  }
}

class Observer {
  constructor(ob) {
    this.ob = ob;
  }
  update(params) {
    console.log(`${params}，通知已传达${this.ob}`);
  }
}

const sb = new Subject();
const ob1 = new Observer("ob1");
const ob2 = new Observer("ob2");
const ob3 = new Observer("ob3");

sb.register(ob1);
sb.register(ob2);
sb.register(ob3);

sb.remove(ob2);

sb.notify("目标发出了通知");
